package com.zp.business.impl.clinicOrder;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.api.prescription.PrescriptionApply;
import com.zp.business.api.prescription.PrescriptionItem;
import com.zp.business.api.prescription.vo.PrescriptionApplyVo;
import com.zp.business.api.prescription.vo.PrescriptionItemVo;
import com.zp.business.entity.clinicOrder.ClinicOrders;
import com.zp.business.entity.clinicOrder.OutpTreatRec;
import com.zp.business.entity.clinicOrder.bo.ClinicOrderParam;
import com.zp.business.entity.clinicOrder.bo.ClinicOrdersBo;
import com.zp.business.entity.clinicOrder.vo.ClinicOrdersCostVo;
import com.zp.business.entity.clinicOrder.vo.ClinicOrdersVo;
import com.zp.business.entity.clinicOrder.vo.OutpTreatRecVo;
import com.zp.business.entity.exam.ExamApply;
import com.zp.business.entity.exam.ExamItem;
import com.zp.business.entity.exam.vo.ExamApplyVo;
import com.zp.business.entity.exam.vo.ExamItemVo;
import com.zp.business.entity.lab.LabApply;
import com.zp.business.entity.lab.LabItem;
import com.zp.business.entity.lab.vo.LabApplyVo;
import com.zp.business.entity.lab.vo.LabItemVo;
import com.zp.business.mapper.clinicOrder.ClinicOrdersMapper;
import com.zp.business.mapper.clinicOrder.OutpTreatRecMapper;
import com.zp.business.mapper.exam.ExamApplyMapper;
import com.zp.business.mapper.exam.ExamItemMapper;
import com.zp.business.mapper.lab.LabApplyMapper;
import com.zp.business.mapper.lab.LabItemMapper;
import com.zp.business.mapper.prescription.PrescriptionApplyMapper;
import com.zp.business.mapper.prescription.PrescriptionItemMapper;
import com.zp.business.service.clinicOrder.IClinicOrdersCostService;
import com.zp.business.service.clinicOrder.IClinicOrdersService;
import com.zp.common.core.constant.BaseConstants;
import com.zp.common.core.constant.CacheNames;
import com.zp.common.core.constant.SerialNumberConstants;
import com.zp.common.core.constant.UserConstants;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.common.redis.utils.CacheUtils;
import com.zp.common.satoken.utils.LoginHelper;
import com.zp.system.api.entity.SysDept;
import com.zp.system.api.entity.SysUser;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 门诊医嘱Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-01-05
 */
@RequiredArgsConstructor
@Service
public class ClinicOrdersServiceImpl implements IClinicOrdersService {

    private final ClinicOrdersMapper baseMapper;
    private final IClinicOrdersCostService clinicOrdersCostService;
    private final LabApplyMapper labApplyMapper;
    private final ExamApplyMapper examApplyMapper;
    private final ExamItemMapper examItemMapper;
    private final LabItemMapper labItemMapper;
    private final PrescriptionApplyMapper prescriptionApplyMapper;
    private final PrescriptionItemMapper prescriptionItemMapper;
    private final OutpTreatRecMapper outpTreatRecMapper;


    /**
     * 用户信息
     */
    static Map<String, SysUser> sysUserMap = CacheUtils.get(CacheNames.SYS_USER);

    static Map<String, SysDept> sysDeptMap = CacheUtils.get(CacheNames.SYS_DEPT);

    /**
     * 查询门诊医嘱
     */
    @Override
    public ClinicOrdersVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询门诊医嘱列表(分页)
     */
    @Override
    public TableDataInfo<ClinicOrdersVo> queryPageList(ClinicOrdersBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ClinicOrders> lqw = buildQueryWrapper(bo);
        Page<ClinicOrdersVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询门诊医嘱列表(不分页)
     */
    @Override
    public List<ClinicOrdersVo> queryList(ClinicOrdersBo bo) {
        LambdaQueryWrapper<ClinicOrders> lqw = buildQueryWrapper(bo);
        List<ClinicOrdersVo> clinicOrdersVos = baseMapper.selectVoList(lqw);
        clinicOrdersVos.forEach(item -> {
            List<ClinicOrdersCostVo> costVoList = new ArrayList<>();
            item.setApplyDeptName(CollectionUtil.isNotEmpty(sysDeptMap) && StringUtils.isNotEmpty(item.getApplyDept())
                    ? sysDeptMap.get(item.getApplyDept()).getDeptName() : "");
            item.setImplementDeptName(CollectionUtil.isNotEmpty(sysDeptMap) && StringUtils.isNotEmpty(item.getImplementDept())
                    ? sysDeptMap.get(item.getImplementDept()).getDeptName() : "");
            item.setApplyUserName(CollectionUtil.isNotEmpty(sysUserMap) && StringUtils.isNotEmpty(item.getApplyUser())
                    ? sysUserMap.get(item.getApplyUser()).getNickName() : "");
            String applyNoPrefix = item.getApplyNo().substring(0, 2);
            switch (Integer.parseInt(applyNoPrefix)) {
                case SerialNumberConstants.CLINIC_EXAM_NO:
                    // 检查
                    ExamApplyVo examApplyVo = examApplyMapper.selectVoOne(Wrappers.<ExamApply>lambdaQuery()
                            .eq(ExamApply::getClinicOrdersId, item.getId()));
                    if (ObjectUtil.isNotNull(examApplyVo)) {
                        // 查询检查明细
                        List<ExamItemVo> examItemVos = examItemMapper.selectVoList(Wrappers.<ExamItem>lambdaQuery()
                                .eq(ExamItem::getApplyId, examApplyVo.getId()));
                        examItemVos.forEach(vo -> {
                            ClinicOrdersCostVo costVo = new ClinicOrdersCostVo();
                            costVo.setItemClass(BaseConstants.ITEM_CLASS.EXAM);
                            costVo.setItemCode(vo.getItemCode());
                            costVo.setPackItemName(vo.getItemName());
                            costVo.setPrice(vo.getPrice());
                            costVo.setCosts(vo.getCosts());
                            costVo.setNum(vo.getNum());
                            costVo.setApplyItemId(vo.getId());
                            costVo.setChargeFlag(vo.getChargeFlag());
                            costVoList.add(costVo);
                        });
                        item.setClinicOrdersCostVoList(costVoList);
                    }
                    break;
                case SerialNumberConstants.CLINIC_LAB_NO:
                    // 检验
                    LabApplyVo labApplyVo = labApplyMapper.selectVoOne(Wrappers.<LabApply>lambdaQuery()
                            .eq(LabApply::getClinicOrdersId, item.getId()));
                    if (ObjectUtil.isNotNull(labApplyVo)) {
                        // 查询检验明细
                        List<LabItemVo> labApplyVos = labItemMapper.selectVoList(Wrappers.<LabItem>lambdaQuery()
                                .eq(LabItem::getApplyId, labApplyVo.getId()));
                        labApplyVos.forEach(vo -> {
                            ClinicOrdersCostVo costVo = new ClinicOrdersCostVo();
                            costVo.setItemClass(BaseConstants.ITEM_CLASS.EXAM);
                            costVo.setItemCode(vo.getItemCode());
                            costVo.setPackItemName(vo.getItemName());
                            costVo.setPrice(vo.getPrice());
                            costVo.setCosts(vo.getCosts());
                            costVo.setNum(vo.getNum());
                            costVo.setApplyItemId(vo.getId());
                            costVo.setChargeFlag(vo.getChargeFlag());
                            costVoList.add(costVo);
                        });
                        item.setClinicOrdersCostVoList(costVoList);
                    }
                    break;
                case SerialNumberConstants.CLINIC_PRESCRIPTION_NO:
                    // 西药、中药
                    PrescriptionApplyVo prescriptionApplyVo = prescriptionApplyMapper.selectVoOne(Wrappers.<PrescriptionApply>lambdaQuery()
                            .eq(PrescriptionApply::getOrdersId, item.getId()));
                    if (ObjectUtil.isNotNull(prescriptionApplyVo)) {
                        // 查询药品明细
                        List<PrescriptionItemVo> prescriptionItemVoList = prescriptionItemMapper.selectVoList(Wrappers.<PrescriptionItem>lambdaQuery()
                                .eq(PrescriptionItem::getApplyId, prescriptionApplyVo.getApplyId()));
                        prescriptionItemVoList.forEach(vo -> {
                            ClinicOrdersCostVo costVo = new ClinicOrdersCostVo();
                            costVo.setItemClass(prescriptionApplyVo.getItemClass());
                            costVo.setItemCode(vo.getDrugCode());
                            costVo.setPackItemName(vo.getDrugName());
                            costVo.setPrice(vo.getPrice());
                            costVo.setCosts(vo.getSumPrice());
                            costVo.setNum(vo.getNum());
                            costVo.setApplyItemId(vo.getItemId());
                            costVo.setChargeFlag(vo.getChargeFlag());
                            costVoList.add(costVo);
                        });
                        item.setClinicOrdersCostVoList(costVoList);
                    }
                    break;
                case SerialNumberConstants.OUTP_TREAT_NO:
                    List<OutpTreatRecVo> outpTreatRecVos = outpTreatRecMapper.selectVoList(Wrappers.<OutpTreatRec>lambdaQuery()
                            .eq(OutpTreatRec::getClinicOrdersId, item.getId())
                    );
                    outpTreatRecVos.forEach(vo -> {
                        ClinicOrdersCostVo costVo = new ClinicOrdersCostVo();
                        costVo.setItemClass(vo.getItemClass());
                        costVo.setItemCode(vo.getItemCode());
                        costVo.setPackItemName(vo.getItemName());
                        costVo.setPrice(vo.getPrice());
                        costVo.setCosts(vo.getSumPrice());
                        costVo.setNum(vo.getNum());
                        costVo.setApplyItemId(vo.getApplyItemId());
                        costVo.setChargeFlag(vo.getChargeFlag());
                        costVoList.add(costVo);
                    });
                    item.setClinicOrdersCostVoList(costVoList);

                    break;
                default:
                    break;
            }
        });
        return clinicOrdersVos;
    }

    private LambdaQueryWrapper<ClinicOrders> buildQueryWrapper(ClinicOrdersBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ClinicOrders> lqw = Wrappers.lambdaQuery();
        lqw.in(CollectionUtil.isNotEmpty(bo.getOrdersIdArray()), ClinicOrders::getId, bo.getOrdersIdArray());
        lqw.eq(StringUtils.isNotBlank(bo.getClinicId()), ClinicOrders::getClinicId, bo.getClinicId());
        lqw.eq(StringUtils.isNotBlank(bo.getPatientId()), ClinicOrders::getPatientId, bo.getPatientId());
        lqw.eq(StringUtils.isNotBlank(bo.getItemClass()), ClinicOrders::getItemClass, bo.getItemClass());
        lqw.eq(StringUtils.isNotBlank(bo.getIsApply()), ClinicOrders::getIsApply, bo.getIsApply());
        lqw.eq(StringUtils.isNotBlank(bo.getChargeFlag()), ClinicOrders::getChargeFlag, bo.getChargeFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getImplementUser()), ClinicOrders::getImplementUser, bo.getImplementUser());
        lqw.eq(StringUtils.isNotBlank(bo.getImplementDept()), ClinicOrders::getImplementDept, bo.getImplementDept());
        lqw.eq(StringUtils.isNotBlank(bo.getGroupNo()), ClinicOrders::getGroupNo, bo.getGroupNo());
        lqw.eq(StringUtils.isNotBlank(bo.getApplyNo()), ClinicOrders::getApplyNo, bo.getApplyNo());
        lqw.eq(StringUtils.isNotBlank(bo.getApplyDept()), ClinicOrders::getApplyDept, bo.getApplyDept());
        lqw.eq(StringUtils.isNotBlank(bo.getFeesNo()), ClinicOrders::getFeesNo, bo.getFeesNo());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), ClinicOrders::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), ClinicOrders::getRemarks, bo.getRemarks());
        lqw.eq(StringUtils.isNotBlank(bo.getIsUpload()), ClinicOrders::getIsUpload, bo.getIsUpload());
        lqw.eq(StringUtils.isNotBlank(bo.getVisitNumber()), ClinicOrders::getVisitNumber, bo.getVisitNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getPaymentNumber()), ClinicOrders::getPaymentNumber, bo.getPaymentNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getNumberType()), ClinicOrders::getNumberType, bo.getNumberType());
        lqw.eq(StringUtils.isNotBlank(bo.getNumberFlag()), ClinicOrders::getNumberFlag, bo.getNumberFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getMergerCharges()), ClinicOrders::getMergerCharges, bo.getMergerCharges());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagnosis()), ClinicOrders::getDiagnosis, bo.getDiagnosis());
        lqw.orderByAsc(ClinicOrders::getCreateTime);
        return lqw;
    }

    /**
     * 新增门诊医嘱
     */
    @Override
    public ClinicOrdersVo insertByBo(ClinicOrdersBo bo) {
        ClinicOrders add = BeanUtil.toBean(bo, ClinicOrders.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return BeanUtil.toBean(bo, ClinicOrdersVo.class);
    }

    /**
     * 修改门诊医嘱
     */
    @Override
    public Boolean updateByBo(ClinicOrdersBo bo) {
        ClinicOrders update = BeanUtil.toBean(bo, ClinicOrders.class);
        update.setUpdateBy(LoginHelper.getUserId());
        update.setUpdateTime(new Date());
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ClinicOrders entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除门诊医嘱
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 门诊项目保存
     *
     * @param param clinicOrder 入参实体类
     * @return
     */
    @Override
    public Boolean saveByBo(ClinicOrderParam param) {
        ClinicOrders clinicOrders = new ClinicOrders();
        return true;
    }

    @Override
    @Transactional
    public ClinicOrders initClinicOrders(ClinicOrderParam param) {
        ClinicOrders clinicOrders = getClinicOrders(param);
        int insert = baseMapper.insert(clinicOrders);
        if (insert > 0) {
            ClinicOrdersBo clinicOrdersBo = BeanUtil.toBean(clinicOrders, ClinicOrdersBo.class);
            // 保存门诊项目明细
            Map<String, Object> map = clinicOrdersCostService.saveByBo(clinicOrdersBo, param.getApplyItemBoList(), param.getNum());
            if (map != null && (Boolean) map.get("flag")) {
                // 应收
                BigDecimal costs = (BigDecimal) map.get("costs");
                // 实收
                BigDecimal paidUpAmount = (BigDecimal) map.get("paidUpAmount");
                // 附加项目类型
                String whetherToComeInAttachedItemClass = String.valueOf(map.get("whetherToComeInAttachedItemClass"));
                // 更新clinic_orders 实收、应收金额
                baseMapper.update(null, new LambdaUpdateWrapper<ClinicOrders>()
                        .set(StringUtils.isNotBlank(whetherToComeInAttachedItemClass), ClinicOrders::getItemClass, whetherToComeInAttachedItemClass)
                        .eq(ClinicOrders::getId, clinicOrders.getId()));
            } else {
                baseMapper.deleteById(clinicOrders.getId());
            }
        }
        return clinicOrders;
    }

    /**
     * 更新clinic_orders 实收、应收金额
     *
     * @param costs                            应收
     * @param paidUpAmount                     实收
     * @param whetherToComeInAttachedItemClass 附加项目类型
     * @param clinicOrders                     门诊医嘱
     */
    @Override
    @Transactional
    public void updateCharges(BigDecimal costs, BigDecimal paidUpAmount, String whetherToComeInAttachedItemClass, ClinicOrders clinicOrders) {
        baseMapper.update(null, new LambdaUpdateWrapper<ClinicOrders>()
                .set(StringUtils.isNotBlank(whetherToComeInAttachedItemClass), ClinicOrders::getItemClass, whetherToComeInAttachedItemClass)
                .eq(ClinicOrders::getId, clinicOrders.getId()));
    }

    @Override
    public ClinicOrders getClinicOrders(ClinicOrderParam param) {
        ClinicOrders clinicOrders = new ClinicOrders();
        clinicOrders.initEntity();
        clinicOrders.setApplyNo(param.getApplyNo());
        clinicOrders.setClinicId(param.getClinicId());
        clinicOrders.setPatientId(param.getPatientId());
        clinicOrders.setItemClass(param.getItemClass());
        clinicOrders.setIsApply(param.getIsApply());
        clinicOrders.setChargeFlag(BaseConstants.CHARGE_FLAG_DICT.ONE);
        clinicOrders.setConfirmFlag(BaseConstants.CONFIRM_FLAG.UNISSUED);
        clinicOrders.setGroupNo("1");
        clinicOrders.setIsUpload(UserConstants.NO);
        clinicOrders.setNumberFlag(UserConstants.NO);
        clinicOrders.setDiagnosis(param.getDiagnosis());
        clinicOrders.setImplementDept(param.getImplementDept());
        clinicOrders.setApplyDept(param.getApplyDept());
        clinicOrders.setApplyUser(param.getApplyUser());
        clinicOrders.setImplementDept(param.getImplementDept());
        clinicOrders.setImplementUser(param.getImplementUser());
        clinicOrders.setOrgId(param.getOrgId());
        clinicOrders.setWhetherToComeInAttached(param.getWhetherToComeInAttached());
        return clinicOrders;
    }

    @Override
    @Transactional
    public Boolean deleteByBo(String orgId, String applyNo) {
        ClinicOrdersBo bo = new ClinicOrdersBo();
        bo.setOrgId(orgId);
        bo.setApplyNo(applyNo);
        List<ClinicOrdersVo> clinicOrdersVos = baseMapper.selectVoList(buildQueryWrapper(bo));
        if (CollectionUtil.isNotEmpty(clinicOrdersVos)) {
            clinicOrdersVos.forEach(item -> {
                // 删除医嘱明细
                clinicOrdersCostService.deleteInOrderId(item.getId());
            });
            return baseMapper.deleteBatchIds(clinicOrdersVos) > 0;
        } else {
            return Boolean.TRUE;
        }
    }

    @Override
    @Transactional
    public boolean update(LambdaUpdateWrapper<ClinicOrders> lambdaUpdateWrapper) {
        return baseMapper.update(lambdaUpdateWrapper) > 0;
    }

    /**
     * 修改收费状态(门诊收费)
     *
     * @param bo
     * @return
     */
    @Transactional
    public boolean updateChargeFlag(ClinicOrdersBo bo) {
        Map<String, Object> columnMap = new HashMap<String, Object>();
        // 修改医嘱主记录收费状态
        baseMapper.update(null, new LambdaUpdateWrapper<ClinicOrders>()
                .set(StringUtils.isNotBlank(bo.getChargeFlag()), ClinicOrders::getChargeFlag, bo.getChargeFlag())
                .set(StringUtils.isNotBlank(bo.getFeesNo()), ClinicOrders::getFeesNo, bo.getFeesNo())
                .eq(ClinicOrders::getId, bo.getId())
        );
        // 修改医嘱明细收费状态
        clinicOrdersCostService.updateChargeType(bo.getChargeFlag(), bo.getId(), BaseConstants.ITEM_REFUND_OR_CANCEL.ALL);
        if (StringUtils.equals(bo.getIsApply(), UserConstants.YES)) {
            // 有申请单
            int applyNoPrefix = Integer.parseInt(bo.getApplyNo().substring(0, 2));
            switch (applyNoPrefix) {
                case SerialNumberConstants.CLINIC_EXAM_NO:
                    examApplyMapper.update(null, new LambdaUpdateWrapper<ExamApply>()
                            .set(ExamApply::getChargeFlag, bo.getChargeFlag())
                            .eq(ExamApply::getClinicOrdersId, bo.getId())
                            .eq(ExamApply::getOrgId, bo.getOrgId())
                    );
                    // 查询主表id用于修改明细收费标识
                    columnMap.put("clinic_orders_id", bo.getId());
                    List<ExamApplyVo> examApplyVos = examApplyMapper.selectVoByMap(columnMap);
                    if (CollectionUtil.isNotEmpty(examApplyVos)) {
                        examItemMapper.update(null, new LambdaUpdateWrapper<ExamItem>()
                                .set(ExamItem::getChargeFlag, bo.getChargeFlag())
                                .eq(ExamItem::getApplyId, examApplyVos.get(0).getId())
                        );
                    }
                    break;
                case SerialNumberConstants.CLINIC_LAB_NO:
                    labApplyMapper.update(null, new LambdaUpdateWrapper<LabApply>()
                            .set(LabApply::getChargeFlag, bo.getChargeFlag())
                            .eq(LabApply::getClinicOrdersId, bo.getId())
                            .eq(LabApply::getOrgId, bo.getOrgId())
                    );
                    // 查询主表id用于修改明细收费标识
                    columnMap.put("clinic_orders_id", bo.getId());
                    List<LabApplyVo> labApplyVos = labApplyMapper.selectVoByMap(columnMap);
                    if (CollectionUtil.isNotEmpty(labApplyVos)) {
                        labItemMapper.update(null, new LambdaUpdateWrapper<LabItem>()
                                .set(LabItem::getChargeFlag, bo.getChargeFlag())
                                .eq(LabItem::getApplyId, labApplyVos.get(0).getId())
                        );
                    }
                    break;
                case SerialNumberConstants.CLINIC_PRESCRIPTION_NO:
                    // 查询处方主表信息
                    PrescriptionApplyVo prescriptionApplyVo = prescriptionApplyMapper.selectVoOne(Wrappers.<PrescriptionApply>lambdaQuery()
                            .eq(PrescriptionApply::getOrdersId, bo.getId())
                            .eq(PrescriptionApply::getOrgId, bo.getOrgId())
                    );
                    prescriptionApplyMapper.update(null, new LambdaUpdateWrapper<PrescriptionApply>()
                            .set(PrescriptionApply::getChargeFlag, bo.getChargeFlag())
                            .eq(PrescriptionApply::getApplyId, prescriptionApplyVo.getApplyId())
                    );
                    prescriptionItemMapper.update(
                            null, new LambdaUpdateWrapper<PrescriptionItem>()
                                    .set(PrescriptionItem::getChargeFlag, bo.getChargeFlag())
                                    .eq(PrescriptionItem::getApplyId, prescriptionApplyVo.getApplyId())
                    );
                    break;
                default:
                    break;
            }
        }
        // 修改非处方沈沁的门诊诊疗项目状态
        if (!StringUtils.equals(bo.getItemClass(), BaseConstants.ITEM_CLASS.WESTERN)
                && !StringUtils.equals(bo.getItemClass(), BaseConstants.ITEM_CLASS.CHINESE)) {
            return outpTreatRecMapper.update(null, new LambdaUpdateWrapper<OutpTreatRec>()
                    .set(OutpTreatRec::getChargeFlag, bo.getChargeFlag())
                    .eq(OutpTreatRec::getClinicOrdersId, bo.getId())
                    .eq(OutpTreatRec::getOrgId, bo.getOrgId())
            ) > 0;
        }
        return true;
    }
}








